package com.airforce.controller;


import com.airforce.mappers.user_awardMapper;
import com.airforce.mappers.wechat_userMapper;
import com.airforce.model.user_award;
import com.airforce.model.wechat_user;
import com.airforce.utils.MD5BASE64Util;
import com.airforce.utils.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RequestMapping(value = "app/user/")
@RestController
public class Wehar_userCon_app {



    @Autowired
    wechat_userMapper wechat_userMapper;

    @Autowired
    user_awardMapper user_awardMapper;


    private  final static Logger logger=LoggerFactory.getLogger(wechatLogin.class);

    //查询用户信息
    @GetMapping(value = "info")
    public Object appuser(@RequestParam String token){

        if(Optional.ofNullable(token).isPresent()) {
            String[] str = MD5BASE64Util.base64decode(token).split(",");
            String openid = str[1];
            wechat_user user = wechat_userMapper.selectByopenid(openid);
            return ResultCode.SUCCESS.result(user);
        }else {
            logger.error("Token为空！");
            return ResultCode.NO_TOKEN.result();
        }

    }


    //查询用户获奖与客服信息
    @GetMapping(value = "useraward")
    public Object useraward(@RequestParam String token){

        if(Optional.ofNullable(token).isPresent()) {
            String[] str = MD5BASE64Util.base64decode(token).split(",");
            String openid = str[1];
            List<Map<String,BigDecimal>>user = user_awardMapper.selectByOpenid(openid);
            wechat_user wechat_user =wechat_userMapper.selectByopenid(openid);
            Map map=new HashMap();
            BigDecimal sum=new BigDecimal(0);
            for(Map<String,BigDecimal> item:user){
                BigDecimal  price=  item.get("price");
                sum=sum.add(price);
            }
            map.put("servicesname",wechat_user.getServicesname());
            map.put("uuid",wechat_user.getUuid());
            map.put("money",sum);

            return ResultCode.SUCCESS.result(map);
        }else {
           logger.error("Token为空！");
           return ResultCode.NO_TOKEN.result();
        }

    }

    @GetMapping("isUser")
    public Object isUser(@RequestParam String token){

        if(Optional.ofNullable(token).isPresent()){
            String[] str = MD5BASE64Util.base64decode(token).split(",");
            String openid = str[1];
            logger.info(openid+"开始领取奖品，停止本次活动");
            wechat_userMapper.updataByisUser(openid);
            return ResultCode.SUCCESS.result();
        }else {
            return  ResultCode.NO_PARM.result();
        }

    }




}
